

/* Test of instrument */


use "G:\Data\Workdata\707677\Common\01Data\02STATA\panel_iv", clear 

cd G:\Data\Workdata\707677\FKYY7677\Debt_relief\Statafiles\Outputs\Revision\FINAL


keep if time==1

tab obs_lawyer

egen lawyer_id_unique=group(lawyerid court) // Unique lawyer id within courts. some lawyers work in multiple courts
bys lawyer_id_unique: egen n_lawyer=count(lawyer_id_unique)
egen mean_lawyer_leniency= mean(granted), by(lawyerid court)

gen iv_judge2_all = (mean_lawyer_leniency*n_lawyer - granted)/(n_lawyer-1)

gen iv_lawyer2=iv_judge2_all-(iv_court*obs_court-granted)/(obs_court-1)





hist n_lawyer
sum n_lawyer, d

keep if n_lawyer>=20

egen n_judge=nvals(lawyerid), by(court aar)

sort  court aar lawyerid iv_lawyer n_judge

order court aar lawyerid iv_lawyer n_judge

tab n_judge

keep if n_judge>1

tab court, gen(courtdum)

tab pyear, gen(yeardum)

egen court_pyear =group(court pyear) 
tab court_pyear






local Xvar_short "male age_0_40 age_41_50 age_51_60 age_61_70 erhvervsindk_mean employed_pstill_mean unemployed_pstill_mean legmarried_m1 racedum1 housedum1 passiv_m1 aktiv_m1 higher_secondary university social_mean timelon_dum1 timelon_dum2 timelon_dum3 timelon_dum4 house_m1"






////////////////////////////////////
// Instrument balance test     /////
////////////////////////////////////


/* Regress instrument on covariates */


reg iv_lawyer2 i.court_pyear `Xvar_short' edu_missing housing_missing, cluster(lawyer_id_unique) 

test `Xvar_short'

local fstat =r(F)
local pstat=r(p)


outreg2 using randomization_results.xls, replace ctitle(Trustee Leniency) drop(i.court_pyear) addstat(F-stat, `fstat', p-value, `pstat')




/* Regress dummy for granted on covariates */


reg granted i.court_pyear `Xvar_short'  edu_missing housing_missing , cluster(lawyer_id_unique) 

test `Xvar_short'


local fstat =r(F)
local pstat=r(p)


outreg2 using randomization_results.xls, append ctitle(Granted) drop(i.court_pyear) addstat(F-stat, `fstat', p-value, `pstat')





/////////////////////////////////////
// First stage regressions   ////////
/////////////////////////////////////



local Xvar_short "male age_0_40 age_41_50 age_51_60 age_61_70 erhvervsindk_mean employed_pstill_mean unemployed_pstill_mean legmarried_m1 racedum1 housedum1 passiv_m1 aktiv_m1 higher_secondary university social_mean timelon_dum1 timelon_dum2 timelon_dum3 timelon_dum4 house_m1"



reg granted iv_lawyer2 i.court_pyear `Xvar_short'  edu_missing housing_missing, cluster(lawyer_id_unique)

test iv_lawyer2

local fstat =r(F)
local pstat=r(p)


outreg2 using First_stage_results.xls, replace ctitle(First stage with controls) drop(i.court_pyear) addstat(F-stat, `fstat', p-value, `pstat')


reg granted iv_lawyer2 i.court_pyear, cluster(lawyer_id_unique)

test iv_lawyer2


local fstat =r(F)
local pstat=r(p)


outreg2 using First_stage_results.xls, append ctitle(First stage without controls) drop(i.court_pyear) addstat(F-stat, `fstat', p-value, `pstat')


* for individuals without missing value


reg granted iv_lawyer2 i.court_pyear if male!=. & age_0_40!=. & age_41_50!=. & age_51_60!=. & age_61_70!=. & erhvervsindk_mean!=. & employed_pstill_mean!=. & unemployed_pstill_mean!=. & legmarried_m1!=. ///
& racedum1!=. & housedum1!=. & passiv_m1!=. & aktiv_m1!=. & higher_secondary!=. & university!=. & social_mean!=. & timelon_dum1!=. & timelon_dum2!=. & timelon_dum3!=. & timelon_dum4!=. & house_m1!=., cluster(lawyer_id_unique)

test iv_lawyer2


local fstat =r(F)
local pstat=r(p)


outreg2 using First_stage_results.xls, append ctitle(First stage without controls same sample) drop(i.court_pyear) addstat(F-stat, `fstat', p-value, `pstat')




//////////////////////////////////////////
/// FIGURE INSTRUMENT      ///////////////
//////////////////////////////////////////



// Non-parametric first stage


// Trustee leniency court x entry year FE

reg iv_lawyer2 i.court_pyear ` Xvar_short'

predict residualized_lawyer, res

histogram residualized_lawyer

histogram iv_lawyer2

reg granted i.court_pyear ` Xvar_short'

predict res_granted, res

sum granted
capture drop mean_granted
gen mean_granted=r(mean)

gen mean_res_granted=res_granted+mean_granted



cd "G:\Data\Workdata\707677\FKYY7677\Debt_relief\Statafiles\Outputs\Revision\FINAL"



lpoly mean_res_granted residualized_lawyer, degree(1) bw(0.04) gen(fs_x fs_y)  n(100) se(se)

gen upper = fs_y + 1.96*se
gen lower = fs_y - 1.96*se

sum iv_lawyer2

egen p99_iv_lawyer2=pctile(iv_lawyer2), p(99)

egen p1_iv_lawyer2=pctile(iv_lawyer2), p(1)




twoway hist residualized_lawyer if abs(residualized_lawyer)<0.15 , width(0.004) yaxis(1) frac fcolor(white) lcolor(gs8) ///
|| line fs_y fs_x if abs(fs_x)<=0.15, yaxis(2) lc(black) ///
|| line upper fs_x if abs(fs_x)<=0.15, yaxis(2) lp(dash) lc(gs8) ///
|| line lower fs_x if abs(fs_x)<=0.15, yaxis(2) lp(dash) lc(gs8) ///
xlabel(-0.15(0.05)0.15, nogrid) ///
graphregion(fcolor(white)) bgcolor(white) scheme(s1color) legend(off) ///
xtitle("Trustee leniency") ///
ytitle("Fraction") ytitle("Pr(granted)", axis(2))

graph export First_stage.pdf, replace






